package cn.iocoder.yudao.module.mqtt.controller.admin.mqtt;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.mqtt.framework.handler.MultiMqttMessageHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * MQTT测试 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - MQTT测试")
@RestController
@RequestMapping("/mqtt/test")
@Validated
public class MqttTestController {

    @Autowired
    private MultiMqttMessageHandler multiMqttMessageHandler;

    @GetMapping("/status")
    @Operation(summary = "获取MQTT消息处理器状态")
    @PreAuthorize("@ss.hasPermission('mqtt:test:query')")
    public CommonResult<String> getHandlerStatus() {
        return success("MQTT消息处理器已启动，正在监听消息...");
    }

    @PostMapping("/simulate-message")
    @Operation(summary = "模拟MQTT消息")
    @PreAuthorize("@ss.hasPermission('mqtt:test:send')")
    public CommonResult<String> simulateMessage(
            @RequestParam(value = "topic", defaultValue = "test/topic") String topic,
            @RequestParam(value = "message", defaultValue = "{\"test\":\"Hello MQTT!\"}") String message) {
        
        // 这里可以模拟消息处理，用于测试
        return success("模拟消息已发送到主题: " + topic + ", 内容: " + message);
    }

    @GetMapping("/debug-headers")
    @Operation(summary = "调试消息头信息")
    @PreAuthorize("@ss.hasPermission('mqtt:test:query')")
    public CommonResult<String> debugHeaders() {
        return success("请查看日志中的INFO级别消息头信息，包括所有消息头键值对");
    }

    @PostMapping("/test-client-id")
    @Operation(summary = "测试客户端ID传递")
    @PreAuthorize("@ss.hasPermission('mqtt:test:send')")
    public CommonResult<String> testClientId(
            @RequestParam(value = "configId", defaultValue = "1") Long configId,
            @RequestParam(value = "topic", defaultValue = "test/client-id") String topic,
            @RequestParam(value = "message", defaultValue = "{\"test\":\"client-id-test\"}") String message) {
        
        // 这里可以测试客户端ID的传递
        return success("测试客户端ID传递 - 配置ID: " + configId + ", 主题: " + topic + ", 消息: " + message);
    }

    @GetMapping("/test-message-headers")
    @Operation(summary = "测试消息头信息")
    @PreAuthorize("@ss.hasPermission('mqtt:test:query')")
    public CommonResult<String> testMessageHeaders() {
        return success("现在每个MQTT实例都有独立的消息处理器，客户端ID和配置ID直接包含在处理器中，无需从消息头获取");
    }

    @GetMapping("/test-instance-handlers")
    @Operation(summary = "测试实例处理器")
    @PreAuthorize("@ss.hasPermission('mqtt:test:query')")
    public CommonResult<String> testInstanceHandlers() {
        return success("每个MQTT实例现在都有独立的消息通道和处理器，客户端ID和配置ID直接可用");
    }
} 